package ljt.music.ui.playlist;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;

import ljt.music.R;
import ljt.music.bean.PlayList;
import ljt.music.bean.SreachBean;

/**
 * @author jiangtao
 * @date 2018/1/2
 */

public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.PlayListHolder> {

  private Context context;
  private PlayList playList;
  private List<SreachBean> sreachBeanList;
  private List<PlayList.PlaylistBean.TracksBean> tracks;
  private OnItemClickLitener mOnItemClickLitener;

  public PlayListAdapter(Context context) {
    this.context = context;
  }

  public PlayList getPlayList() {
    return playList;
  }

  public List<SreachBean> getSearchList() {
    return sreachBeanList;
  }

  public void setPlayList(PlayList playList) {
    this.sreachBeanList = null;
    this.playList = playList;
    PlayList.PlaylistBean playlist = playList.getPlaylist();
    if (playlist != null) {
      tracks = playlist.getTracks();
      notifyDataSetChanged();
    }
  }

  public void setSearchList(List<SreachBean> sreachBeanList) {
    this.playList = null;
    this.sreachBeanList = sreachBeanList;
    notifyDataSetChanged();
  }

  public interface OnItemClickLitener {
    void onItemClick(View view, int position);

    void onItemLongClick(View view, int position);
  }

  public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
    this.mOnItemClickLitener = mOnItemClickLitener;
  }

  @Override
  public PlayListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new PlayListHolder(LayoutInflater.from(context).inflate(R.layout.music_item, parent, false));
  }

  @Override
  public void onBindViewHolder(PlayListHolder holder, int position) {
    if (playList != null) {
      PlayList.PlaylistBean.TracksBean tracksBean = tracks.get(position);
      holder.name.setText(tracksBean.getName() + "-" + tracksBean.getAr().get(0).getName());
      holder.album.setText("《" + tracksBean.getAl().getName() + "》");
      Glide.with(context)
          .load(tracksBean.getAl().getPicUrl())
          .crossFade()
          .diskCacheStrategy(DiskCacheStrategy.ALL)
          .into(holder.cover);
    } else if (sreachBeanList !=null) {
      SreachBean sreachBean = sreachBeanList.get(position);
      holder.name.setText(sreachBean.getName() + "-" + sreachBean.getArtist().get(0));
      holder.album.setText("《" + sreachBean.getAlbum() + "》");
      Glide.with(context)
          .load("http://www.nmedia.net.cn/N_Editor/userfiles/images/20174259481117867.jpg")
          .crossFade()
          .diskCacheStrategy(DiskCacheStrategy.ALL)
          .into(holder.cover);
    }

    // 如果设置了回调，则设置点击事件
    if (mOnItemClickLitener != null) {
      holder.itemView.setOnClickListener(v -> {
        int pos = holder.getLayoutPosition();
        mOnItemClickLitener.onItemClick(holder.itemView, pos);
      });

      holder.itemView.setOnLongClickListener(v -> {
        int pos = holder.getLayoutPosition();
        mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
        return false;
      });
    }
  }

  @Override
  public int getItemCount() {
    if (playList != null) {
      return tracks != null ? tracks.size() : 0;
    } else if (sreachBeanList != null) {
      return sreachBeanList.size();
    }
    return 0;
  }

  class PlayListHolder extends RecyclerView.ViewHolder {
    ImageView cover;
    TextView name;
    TextView album;

    public PlayListHolder(View view) {
      super(view);
      cover = view.findViewById(R.id.cover);
      name = view.findViewById(R.id.name);
      album = view.findViewById(R.id.album);
    }
  }
}
